package org.snail.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//统一格式化返回的时期格式
@Component("CustomDateSerializerList")
public class CustomDateSerializerList extends JsonSerializer<List<Date>> {  //

    @Override
    public void serialize(List<Date> value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
       String dates = "";
        for(Date date : value){
            if(date!=null){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                dates=dates+sdf.format(date)+"~";
            }
       }
        if(!"".equals(dates)){
            dates = dates.substring(0,dates.length()-1);
        }
        gen.writeString(dates);
    }
}